﻿/*
Copyright (C) 2011 by Matthew Hickson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using System.Collections;

namespace thinkschematwo.Collections
{
    class GroupedList
    {

        private String GLOBAL_KEY = ""; // TODO: write a copy constructor to preserve this...
        private Hashtable groups = null;

        public GroupedList()
        {
            GLOBAL_KEY = Guid.NewGuid().ToString(); // decrease opportunity for collisions with library users
            groups = new Hashtable();

            AddGroup(GLOBAL_KEY);
        }

        public ArrayList GroupKeys {
            get {
                ArrayList keys = new ArrayList(groups.Keys);

                // Handle global key (don't want to expose the Guid...)
                keys.Remove(GLOBAL_KEY);
                keys.Add("");

                return keys;
            }
        } // GroupKeys

        public bool ExistsGroup(String groupName)
        {
            return (groups.ContainsKey(groupName));
        } // ExistsGroup

        public void AddGroup(String groupName)
        {
            if (!ExistsGroup(groupName))
            {
                groups.Add(groupName, new ArrayList());
            }
        } // AddGroup

        public void RemoveGroup(String groupName)
        {
            if (ExistsGroup(groupName))
            {
                groups.Remove(groupName);
            }
        } // RemoveGroup

        public void ClearGroup(String groupName)
        {
            if (ExistsGroup(groupName))
            {
                ArrayList datalist = GetDataList(groupName);
                datalist.Clear();
            }
        } // ClearGroup

        // TODO: Confirm this is the way I want to return this... (value/reference/etc.)
        public ArrayList GetDataList(String groupName = "")
        {
            if (String.IsNullOrEmpty(groupName)) { groupName = GLOBAL_KEY; }

            if (ExistsGroup(groupName))
            {
                return (ArrayList)groups[groupName];
            }
            else
            {
                return null;
            }
        } // GetDataList

        public bool ExistsDataItem(String value, String groupName = "")
        {
            if (String.IsNullOrEmpty(groupName)) { groupName = GLOBAL_KEY; }

            if (ExistsGroup(groupName))
            {
                ArrayList datalist = GetDataList(groupName);
                if (datalist.Contains(value))
                {
                    return true;
                }
            }

            // Default return
            return false;
        } // ExistsDataItem

        public void AddDataItem(String value, String groupName = "")
        {
            if (String.IsNullOrEmpty(groupName)) { groupName = GLOBAL_KEY; }

            if (!ExistsDataItem(value, groupName))
            {
                ArrayList datalist = GetDataList(groupName);
                datalist.Add(value);
            }
        } // AddDataItem

        public void RemoveDataItem(String value, String groupName = "")
        {
            if (String.IsNullOrEmpty(groupName)) { groupName = GLOBAL_KEY; }

            if (!ExistsDataItem(value, groupName))
            {
                ArrayList datalist = GetDataList(groupName);
                datalist.Remove(value);
            }
        } // RemoveDataItem

    }
}
